/* 	@Author Gabriel Henrique
	@Author Handel Camilo
	@Author Bruno Miranda
 */


%% 


%{
	private int comment_count = 0;
	private int str_cont;
%} 

%{
	private Parser yyparser;
	
	StringBuffer string = new StringBuffer();
	public Yylex(java.io.Reader r, Parser yyparser) {
		this(r);
		this.yyparser = yyparser;
	}
	
	int getLineNum () {
		return (yyline+1);
	}
	
	boolean getDebug(){
		return yyparser.yydebug;
	}
	
	
%}


%eof{
	if(comment_count != 0) {
		System.out.println("\n" + "ERRO: COMENTARIO NAO TERMINA");
	}
	if(str_cont != 0) {
		System.out.println("/n" + "ERRO: CADEIA DE CARACTERES NAO TERMINA"); 
	}	
%eof}


%line 
%column
%state COMMENT
%state STRING
%state CHAR
%full  /* cause the generated scanner to use an 8 bit input character set */
%byaccj


Espaco_em_Branco = [ \t\n]+
Nova_Linha = \r|\n|\r\n
NUM = 0 | [1-9][0-9]*
Letras = [A-Za-z]
ID = {Letras}({Letras}|{NUM}|_)*
Str = \"   /*conststring*/
Chr = \'  /*constchar*/
 
nothing = .
String = [^\r\n\"\\]



%%

<YYINITIAL> {

	","					{ System.out.println(", "  + yytext() + " " + yyline); return yyparser.VIRGULA; }
	";"					{ System.out.println("; "  + yytext() + " " + yyline); return yyparser.PONTOVIRGULA; }
	"("					{ System.out.println("( "  + yytext() + " " + yyline); return yyparser.PARENTESEESQUERDO; }
	")"					{ System.out.println(") "  + yytext() + " " + yyline); return yyparser.PARENTESEDIREITO; }
	"["					{ System.out.println("[ "  + yytext() + " " + yyline); return yyparser.COLCHETEESQUERDO; }
	"]"					{ System.out.println("] "  + yytext() + " " + yyline); return yyparser.COLCHETEDIREITO; }
	"{"					{ System.out.println("{ "  + yytext() + " " + yyline); return yyparser.CHAVEESQUERDO; }
	"}"					{ System.out.println("} "  + yytext() + " " + yyline); return yyparser.CHAVEDIREITO; }
	"+"					{ System.out.println("+ "  + yytext() + " " + yyline); return yyparser.OPADICAO; }
	"-"					{ System.out.println("- "  + yytext() + " " + yyline); return yyparser.OPSUBTRACAO; }
	"*"					{ System.out.println("* "  + yytext() + " " + yyline); return yyparser.OPMULT; }
	"/"					{ System.out.println("/ "  + yytext() + " " + yyline); return yyparser.OPDIVISAO; }
	"="					{ System.out.println("= "  + yytext() + " " + yyline); return yyparser.ATRIBUICAO; }
	"<"					{ System.out.println("< "  + yytext() + " " + yyline); return yyparser.MENOR; }
	"<="				{ System.out.println("<= " + yytext() + " " + yyline); return yyparser.MENORIGUAL; }
	">"					{ System.out.println("> "  + yytext() + " " + yyline); return yyparser.MAIOR; }
	">="				{ System.out.println(">= " + yytext() + " " + yyline); return yyparser.MAIORIGUAL; }
	"||"				{ System.out.println("|| " + yytext() + " " + yyline); return yyparser.OR; }
	"!"					{ System.out.println("! "  + yytext() + " " + yyline); return yyparser.NEGACAO; }
	
	"&&"				{ System.out.println("&& " + yytext() + " " + yyline); return yyparser.AND; }
	"=="				{ System.out.println("== " + yytext() + " " + yyline); return yyparser.IGUALDADE; }
	"!="				{ System.out.println("!= " + yytext() + " " + yyline); return yyparser.DIFERENCA; } 
	
	{Espaco_em_Branco}	{/* ignora */}
	{Nova_Linha}		{/* ignora */} 
	"int"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.INT; }
	"char"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.CHAR; }
	"return"			{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.RETURN;  }
	"read"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.READ;  }
	"write"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.WRITE;  }
	"writeln"			{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.WRITELN;  }
	"break"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.BREAK;  }
	"if"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.IF;  }
	"else"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.ELSE; }
	"while"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); return yyparser.WHILE;  }
	"main"				{ System.out.println("PALAVRA RESERVADA " + yytext() + " " + yyline); yyparser.yylval = new ParserVal(yytext());
																							 return yyparser.MAIN;}
	{ID}				{ System.out.println("ID "  + yytext() + " " + yyline ); 
						  yyparser.yylval = new ParserVal(yytext());
						  return Parser.ID; }
	{NUM}				{ System.out.println("NUM " + yytext() + " " +  yyline); 
						  yyparser.yylval = new ParserVal(Integer.parseInt(yytext()));	
						  return Parser.NUM;
						}
	"/*"				{ yybegin(COMMENT); comment_count++; }
	{Str}				{ str_cont++;  yybegin(STRING); }
	
	{Chr}				{ str_cont++;  yybegin(CHAR); }
	{nothing}			{ System.out.println("ERRO: Caractere Invalido: " + yytext() + " " + yyline); } 
	
	
}/* fim do <YYINITIAL> */

  
  
<COMMENT> {
	
	"/*"				{ comment_count++; }
	"*/"				{ if (--comment_count == 0) yybegin(YYINITIAL); }
	"," 				{/*ignora*/ }
	";"					{/*ignora*/ }
	"("					{/*ignora*/ }
	")"					{/*ignora*/ }
	"["					{/*ignora*/ }
	"]"					{/*ignora*/ }
	"{"					{/*ignora*/ }
	"}"					{/*ignora*/ }
	"+"					{/*ignora*/ }
	"-"					{/*ignora*/ }
	"*"					{/*ignora*/ }
	"/"					{/*ignora*/ }
	"="					{/*ignora*/ }
	"<"					{/*ignora*/ }
	">"					{/*ignora*/ }
	"&"					{/*ignora*/ }
	"|"					{/*ignora*/ }
	"!"					{/*ignora*/ }
	{NUM}				{/*ignora*/ }
	{Letras}			{/*ignora*/ }
	{Espaco_em_Branco}	{/*ignora*/ }
	{Nova_Linha}		{/*ignora*/ } 
	{nothing}			{/*ignora*/  } 

} /*fim do <COMMENT> */



<STRING> {

	{String}			{ String str = yytext().substring(1,yytext().length()); }
	{Nova_Linha}		{ System.out.println("ERRO: CADEIA DE CARACTERES OCUPA MAIS DE UMA LINHA");}
	\"					{	if (--str_cont == 0) yybegin(YYINITIAL);
							System.out.println("STR " + yytext() + " " +  yyline);
							yyparser.yylval = new ParserVal(yytext());
							return Parser.STR;
						}

	{nothing}			{ System.out.println("ERRO: Caractere Invalido: " + yytext() + " " + yyline); } 

} /*fim do <STRING>*/


<CHAR> {
			
	{Letras}			{ String str = yytext().substring(1,yytext().length()); }				

	\'					{ if (--str_cont == 0) yybegin(YYINITIAL);
						  System.out.println("CHAR " + yytext() + " " + yyline); 
						  yyparser.yylval = new ParserVal(yytext());
						  return Parser.CCHAR;
						}
	{String}			{ String str = yytext().substring(1,yytext().length()); }						

	{nothing}			{ System.out.println("ERRO: Caractere Invalido: " + yytext() + " " + yyline); } 

}/*fim do <CHAR>*/

 
 












  
